<template>
<div class="container">
    <div class="page-head">
        <div class="left">
            <el-form :inline="true" :model="query" @submit.native.prevent>
                <el-form-item>
                    <el-button type="danger" @click="showCreateView"><i class="el-icon-plus" style="margin-right: 5px"></i>添加</el-button>
                </el-form-item>
                <el-form-item>
                    <picker-view v-model="query.company_id" type="company" placeholder="请选择所属公司"></picker-view>
                </el-form-item>
                <el-form-item>
                    <picker-view v-model="query.supplier_id" type="supplier" placeholder="请选择供应商"></picker-view>
                </el-form-item>
                <el-form-item>
                    <picker-view v-model="query.category_id" type="category" placeholder="请选择商品分类"></picker-view>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="客户姓名 / 手机号" clearable></el-input>
                </el-form-item>
                <el-button type="primary" native-type="submit" @click="doSearch">搜索</el-button>
            </el-form>
        </div>
    </div>

    <el-table ref="tableView" height="tableHeight" class="page-body" :data="tableData" border>
        <el-table-column label="商品ID" prop="id" width="100"></el-table-column>
        <el-table-column label="所属公司" prop="company.title"></el-table-column>
        <el-table-column label="供应商" prop="supplier.title"></el-table-column>
        <el-table-column label="商品分类" prop="category.title"></el-table-column>
        <el-table-column label="商品名称" prop="title"></el-table-column>
        <el-table-column label="商品售价" prop="price"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
                <el-tag v-if="scope.row.status==1" type="primary">使用中</el-tag>
                <el-tag v-else type="danger">已下架</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button type="primary" @click="showEditView(scope.row)">编辑</el-button>
                <el-button type="danger" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="page-foot">
        <div class="left">

        </div>
        <div class="right">
            <common-pagination :total="pg.total" :page.sync="pg.page" :limit.sync="pg.limit" @getList="getTableData"></common-pagination>
        </div>
    </div>

    <!-- 添加客资 -->
    <save-view ref="saveView" @getTableData="getTableData">></save-view>
</div>
</template>

<script>
import pagination from "@/components/mixins/pagination.vue";
import pickerView from "@/components/picker/select.vue";
import saveView from './save.vue';

import {
    getPageDataApi,
    deleteProduceApi
} from "@/api/product/produce";

const query = {
    company_id: "",
    supplier_id: "",
    category_id: "",
    source_id: "",
    keywords: ""
}

export default {
    components: {
        pickerView,
        saveView
    },
    mixins: [pagination],
    data() {
        return {
            query: Object.assign({}, query),
            tableData: [],
        }
    },
    created() {
        this.getTableData();
    },
    methods: {
        doSearch(){
            this.resetPg();
            this.getTableData();
        },
        getTableData() {
            let params = Object.assign({}, this.query);
            params["pagination"] = this.pg;
            getPageDataApi(params).then(res => {
                if (res.code == 200) {
                    this.tableData = res.data.list;
                    this.setPg(res);
                } else {
                    this.$message.warning(res.msg);
                }
            })
        },
        showCreateView() {
            this.$refs['saveView'].showCreate();
        },
        showEditView(row) {
            this.$refs["saveView"].showEdit(row);
        },
        deleteRow(row) {
            this.$confirm("确认删除该条信息?", "警告", {
                    type: "warning",
                })
                .then((res) => {
                    deleteProduceApi(row)
                        .then((res) => {
                            if (res.code == 200) {
                                this.getTableData();
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.info("系统异常，请联系管理员");
                        });
                })
                .catch((res) => {
                    this.$message.info("操作已取消");
                });
        },
    }
}
</script>

<style>

</style>
